package JDBC_Test.book.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import JDBC_Test.book.JDBCUtil;
import JDBC_Test.book.Model.CardModel;

public class CardDao {

	Connection con = null;
	Statement st = null;
	PreparedStatement ps = null;

	/**
	 * 办卡
	 * 
	 * @param cardModel
	 * @throws SQLException
	 */
	public void insert(CardModel cardModel) throws SQLException {
		// TODO Auto-generated method stub
		String sql = "insert into card (card_no) values(?)";

		try {
			con = JDBCUtil.geConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, cardModel.getCardNo());
			ps.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			JDBCUtil.close(con, st, ps);
		}

	}

	/**
	 * 查询输入的卡号是否存在
	 * 
	 * @param cardModel
	 * @return
	 * @throws SQLException
	 */
	public boolean select(CardModel cardModel) throws SQLException {
		// TODO Auto-generated method stub
		String sql = "select * from  card where card_no = ?";
		try {
			con = JDBCUtil.geConnection();
			ps = con.prepareStatement(sql);
			ps.setString(1, cardModel.getCardNo());

			ResultSet rs = ps.executeQuery();
			int count = 0;
			while (rs.next()) {
				count++;
			}
			if (count != 0) {
				return true;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			JDBCUtil.close(con, st, ps);

		}
		return false;
	}

}
